from base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from order_page import OrderPage


class LoginPage(BasePage):
    __url = "https://www.kongfz.com/"
    __username = (By.CSS_SELECTOR, "#username")
    __password = (By.CSS_SELECTOR, "#password")
    __loginText = (By.CSS_SELECTOR, "#nickName>span")
    __loginBtn = (By.CSS_SELECTOR, ".login_submit.btn_red_h40")
    __logindialog = (By.CSS_SELECTOR, "#loginWin>div>div.login-btn")
    __iframeLogin = (By.CSS_SELECTOR, "#iframe_login")
    __nickName = (By.CSS_SELECTOR, "#nickName>span")
    __myOrder = (By.CSS_SELECTOR, ".user-info-box.clearfix>div:nth-child(4)>a>span")

    def openLoginPage(self):
        self.driver.get(self.__url)

    def input_userName(self, userNaame):
        self.find_element(*self.__username).send_keys(userNaame)

    def input_passWord(self, password):
        self.find_element(*self.__password).send_keys(password)

    def click_loginBtn(self):
        self.find_element(*self.__loginBtn).click()

    def get_nickNametext(self):
        return self.find_element(*self.__nickName).text

    def click_myorder(self):
        self.find_element(*self.__myOrder).click()

    def open_logindialog(self):
        ActionChains(self.driver).move_to_element(self.find_element(*self.__loginText)).perform()
        self.find_element(*self.__logindialog).click()
        sleep(2)
        iframe = self.driver.find_element(*self.__iframeLogin)
        self.driver.switch_to.frame(iframe)

    def open_orderPage(self):
        self.click_myorder()
        sleep(2)
        return OrderPage(self.driver)

    def login_gjs_pro(self, username, password):
        self.open_logindialog()
        self.input_userName(username)
        self.input_passWord(password)
        self.click_loginBtn()
        sleep(2)
